const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
    stats: {
        builtAt: false, // 添加构建日期与时间信息
        timings: false, // 时间信息
        hash: false,
        version: false,
        modules: false, // 添加关于构建模块的信息
        moduleTrace: false, // 展示依赖和告警/错误的来源
        chunks: false, // Makes the build much quieter
        colors: true,
        children: false, // 显示子进程日志
    },
    entry: path.resolve(__dirname, './src/main.js'),
    output: {
        filename: '[name].[hash:9].js',
        path: path.resolve(__dirname, './dist'),
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['vue-style-loader', 'css-loader'],
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    hotReload: true,
                    transformAssetUrls: {
                        source: 'src',
                        img: 'src',
                    },
                },
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    {
                        loader: 'postcss-loader',
                        options: {
                            postcssOptions: {
                                plugins: [
                                    [
                                        'autoprefixer',
                                        {
                                            browsers: [
                                                'last 2 version',
                                                '>1%',
                                                'iOS 7',
                                            ],
                                        },
                                    ],
                                ],
                            },
                        },
                    },
                    'less-loader',
                ],
            },
            {
                test: /\.(jpg|png)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        esModule: false,
                        limit: 25000,
                    },
                },
            },
            {
                test: /\.svg$/,
                use: 'file-loader',
            },
            {
                test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 50000,
                        mimetype: 'application/font-woff',
                        name: './fonts/[name].[ext]',
                    },
                },
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                        plugins: [
                            '@babel/plugin-proposal-object-rest-spread',
                            '@babel/plugin-syntax-dynamic-import',
                            '@babel/plugin-proposal-class-properties',
                            '@babel/plugin-proposal-nullish-coalescing-operator',
                            '@babel/plugin-proposal-optional-chaining',
                            '@babel/plugin-proposal-private-methods',
                        ],
                    },
                },
            },
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            favicon: path.resolve(__dirname, './public/favicon.ico'),
            template: path.resolve(__dirname, './public/index.html'),
            publicPath: './',
        }),
        new HtmlWebpackExternalsPlugin({
            externals: [
                {
                    module: 'vue',
                    entry: 'https://lib.baomitu.com/vue/2.6.12/vue.min.js',
                    global: 'Vue',
                },
            ],
        }),
        new VueLoaderPlugin(),
        new CleanWebpackPlugin(),
    ],
    externals: {
        vue: 'Vue',
    },
}
